package live.common;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


public class ReaderXmlFile {
	private SAXReader saxReader = null;
	private File file = null;
	private Document document = null;
	private int index = 1;
	private JSONArray jsonArray = null;

	public JSONArray returnXmlContent(String filePath, String qName,
			String attr, String value) {
		try {
			saxReader = new SAXReader();
			file = new File(filePath);
			if (file.exists()) {
				document = saxReader.read(file);
				Element element = document.getRootElement();
				Iterator iterator = element.elementIterator(qName);
				if (iterator != null) {
					jsonArray = new JSONArray();
					JSONObject obj = null;
					while (iterator.hasNext()) {
						element = (Element) iterator.next();
						if (element.attributeValue("CityCode").trim().equals(
								value)) {
							obj = new JSONObject();
							obj.put("area", element.attributeValue(attr));
							obj.put("index", element.attributeValue(attr));
							jsonArray.add(obj);
							index++;
						}
					}
				}
			}
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			index = 1;
			document = null;
			file = null;
			saxReader = null;
		}
		return jsonArray;
	}
	
	public enum xmlFileName {
		BUSINESS {
			public String toString() {
				return ReaderXmlFile.class.getResource("/").getPath()
						+ "Staticdata/Business.xml";
			}
		},
		REGION {
			public String toString() {
				return ReaderXmlFile.class.getResource("/").getPath()
						+ "Staticdata/Region.xml";
			}
		}
	}

}
